package com.xiaohu.demo.utils.longLink.client;

import java.util.HashMap;
import java.util.Map;

import io.netty.channel.ChannelHandlerContext;
/**
*继承我们自己编写的枢纽站
*/
public class Client3Handler extends Middleware {

    private Client client;

    public Client3Handler(Client client) {
        super("client");
        this.client = client;
    }

    @Override
    protected void handlerData(ChannelHandlerContext ctx, Object msg) {
        // TODO Auto-generated method stub
    	Map<String,String> his = new HashMap<>();
    	his.put("3714231991RegNo", "3714231991url4,3714231991url5");
    	his.put("3714231995RegNo", "3714231995url1,3714231995url2");
        Model model = (Model) msg;
        int type = model.getType();
        String body = model.getBody();
        if(type == TypeData.CUSTOMER) {
        	System.out.println("内网前置机获取到外网前置机的登记号body： " + body);
        	String ftpUrl = "";
        	String ftpUrlStr = "";
        	if(body!=null && !"".equals(body)) {
        		String keys[] = body.split(",");
        		int i = 0;
        		for(String key:keys) {
        			ftpUrl = his.get(key);
        			ftpUrlStr += key+"_"+ftpUrl;
        			if(i<keys.length-1) {
        				ftpUrlStr += "^";
        			}
        			i++;
        		}
        	}
        	/*拿着登记号调用his接口获取图片*/
        	model.setBody(ftpUrlStr);
        	ctx.channel().writeAndFlush(model);
        }
    }
    @Override
    protected void handlerAllIdle(ChannelHandlerContext ctx) {
        // TODO Auto-generated method stub
        super.handlerAllIdle(ctx);
        sendPingMsg(ctx);
    }   
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        super.channelInactive(ctx);
        client.doConnect();
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {      
        System.out.println(name + "exception :"+ cause.toString());
    }
}
